//选择审批级数时的变化
jQuery('#auditorNumber').change(function() {
	var n = jQuery(this).val();
	jQuery("input[name*='ai']").each(function() {
		var index = jQuery(this).prop('id').substr(-1);
		var parent = jQuery(this).parent().parent();
		if (index > n)
			parent.addClass('hide');
		else
			parent.removeClass('hide');
	});
});
// 合同提交审批
jQuery(".app-btn-submit").on(
		'click',
		function() {

			var num = jQuery("#auditorNumber").val();
			for(var i=1;i<=num;i++){
				var val= jQuery("#ai"+i).val();
				if(!val){
					message('审批人没有指定！');
					return;
				}
			}
	
			
			
			
			if (contractCode==""){
				contractCode = jQuery('#supplierCode').val();
			}
			$.springLemonDialog({
				content : '确认提交审批：' + contractCode + '？',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					// index:0 确认 1：取消
					// sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
					if (index == 0) {
						var flag = 0;
						if(jQuery("#contractAtrrForm") 
								&& !jQuery("#contractAtrrForm").Validform({
									tiptype:3,
									showAllError:true
									}).check(false)){
							flag = 1;
				        };
						var n = jQuery('#auditorNumber').val();
						jQuery("input[name*='ai']").each(function() {
							var index = jQuery(this).prop('id').substr(-1);
							var parent = jQuery(this).parent().parent();
							if (index <= n) {
								if (jQuery(parent)
										&& !jQuery(parent).Validform({
											tiptype : 3,
											showAllError : true
										}).check(false)) {
									flag = 1;
								}
								;
							}
						}); 
						if (flag==0){
							saveContract();
						}

					}
					$(this).closeDialog(modal);
				}
			});

		});

// 引入审批 信息弹窗
jQuery('.btn-find-auditor').on("click", function() {
	currentAuditor = jQuery(this).prop('id');
	jQuery("#modal-find-auditor").modal('show');
	jQuery("#modal-find-auditor input[type='radio']").attr('checked', false);
});

// 用户查找弹窗
var serverUserData = {};
serverUserTable = jQuery('#table-user')
		.DataTable(
				{
					"serverSide" : true,
					"iDisplayLength" : 4,
					"ordering" : false,
					"ajax" : {
						"dataType" : "json",
						"type" : "get",
						"cache" : false,
						"url" : window.rootPath + '/ui/user/getpage',
						"data" : function(d) {
							return $.extend({}, d, serverUserData);
						}
					},
					columns : [
							{
								// 1序号
								data : null,
								defaultContent : ""
							},
							{// 2姓名
								data : "empLogin.name",
							},
							{// 3性别
								data : 'sex'
							},

							{// 4职务
								data : 'position'
							},
							{// 5销售区域
								data : 'saleArea.superiorArea'
							},
							{// 6选择按钮
								data : null,
								render : function() {
									var html = "<input type='radio' name='btn-select' class='app-select-user'/>"
									return html;
								}
							}

					],
					"drawCallback" : function(oSettings) {
						var k = 0;
						var total = oSettings.aiDisplay.length
								+ oSettings._iDisplayStart;

						for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
							$(
									'td:eq(0)',
									oSettings.aoData[oSettings.aiDisplay[k]].nTr)
									.text(i + 1);
							k++;
						}

						serverUserTable.off('click', 'tr');
						serverUserTable
								.on(
										'click',
										'tr',
										function(event) {
											event = event || window.event;
											var target = event.target
													|| event.scrElement;
											var data = serverUserTable
													.row(this).data();

											if (target.className
													.indexOf('app-select-user') != -1) {
												onSelectUser(data);
											}
											return;
										});
					}
				});// end of datatable
var selectedUser = null;
var currentAuditor = null;
var onSelectUser = function onSelectUser(data) {
	selectedUser = data;
};

// 引入用户按钮
jQuery('#btn-reference').on('click', function() {
	if (selectedUser && currentAuditor) {
		jQuery("#ai" + currentAuditor).val(selectedUser.empLogin.name);
		jQuery("#auditor" + currentAuditor).val(selectedUser.id);
	}

	selectedUser = null;
	currentAuditor = null;
	jQuery("#modal-find-auditor").modal('hide');
});
// 查找用户返回按钮
jQuery('.app-find-user-return').on('click', function() {
	selectedUser = null;
	currentAuditor = null;
});
// 查询
jQuery('.app-btn-search').on('click', function() {
	serverUserData.key = jQuery("#search").val();
	serverUserTable.draw();
});
// 撤销查询
jQuery('.app-btn-reset').on('click', function() {
	jQuery('#search').val('');
	serverUserData.key = '';
	serverUserTable.draw();
});

/**
 * 保存合同
 */
var saveContract = function() {
	var signedDate = jQuery('#signedDate').val();
	var giveOutDate = jQuery('#giveOutDate').val();
	var scheduleDate = jQuery('#scheduleDate').val();
	var operatDate = jQuery('#operatDate').val();
	if (signedDate==""||giveOutDate==""||operatDate==""){
		message("请将页面上所有日期信息完善，保存后还可进行编辑。");
		return ;
	}
    var isId=jQuery("#contractId").val();
    var contractcreateTime=jQuery("#createTime").val();
	var createTime=new Date(contractcreateTime).getTime() ;
    var supplierCode = jQuery('#supplierCode').val();
	var demanderCode = jQuery('#demanderCode').val();
	var contractType = jQuery('#contractType').val();
	var totalSum = jQuery('#totalSum').val();
	var stop = jQuery('#stop').val();
	var freightCharge = jQuery('#freightCharge').val();
	var projectName = jQuery('#projectName').val();
 	var payType = jQuery('#payType').val();
 	var guaranteePeriod = jQuery('#guaranteePeriod').val();
 	var breakDuty = jQuery('#breakDuty').val();
 	var customerId = jQuery("#customerId").val();
	var orderUnit = jQuery('#orderUnit').val();
 	var orderDelegate = jQuery('#orderDelegate').val();
	var orderPhone = jQuery('#orderPhone').val();
	var industryInfo = jQuery('#industryInfo').val();
	var orderAddress = jQuery('#orderAddress').val();
	var orderZip = jQuery('#orderZip').val();
	var orderArea = jQuery('#orderArea').val();
	var invoiceBank = jQuery("#invoiceBank").val();
	var bankAccount = jQuery("#bankAccount").val();
	var taxNumber = jQuery("#taxNumber").val();
	var invoiceAddress = jQuery("#invoiceAddress").val();
	var postUnit = jQuery('#postUnit').val();
 	var postDelegate = jQuery('#postDelegate').val();
	var postPhone = jQuery('#postPhone').val();
	var postAddress = jQuery('#postAddress').val();
	var postZip = jQuery('#postZip').val();
	var dutyArea = jQuery('#dutyArea').val();
	var dutyOfficer = jQuery('#dutyOfficer').val();
	var southNorthArea = jQuery('#southNorthArea').val();
	var belongArea = jQuery('#belongArea').val();
	var checkKind = jQuery('#checkKind').val();
	var productKind = jQuery('#productKind').val();
	var signOfficer = jQuery('#signOfficer').val();
	var repairOfficer = jQuery('#repairOfficer').val();
	var quotationOfficer = jQuery('#quotationOfficer').val();
	var operatDepartment = jQuery('#operatDepartment').val();
	var operator = jQuery('#operator').val();
	var memo = jQuery('#memo').val();
	var state = jQuery('#state').val();
	var cstate = jQuery('#cstate').val();
	if (contractType=="项目合同"){
		var scheduleDate = jQuery('#scheduleDate').val();
		if (scheduleDate==""){
			message("请将排产日期信息完善，保存后还可进行编辑。");
	 		return ;
		}
		var contract = "saveContract";
		// 判断是编辑还是新增
        if(isId == ""){// 新增合同
        	var d = {
 					"user":{"id":$("#userId").val()},
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }else{
        	var d = {
        			"id": isId,
	        		"createTime": createTime,
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }
	}else if (contractType=="备件合同"){
		var projectCode = jQuery('#projectCode').val();
		var contract = "saveContractPart";
		// 判断是编辑还是新增
        if(isId == ""){// 新增合同
        	var d = {
        			"user":{"id":$("#userId").val()},
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }else{
        	var d = {
        			"id": isId,
	        		"createTime": createTime,
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }
	}else if (contractType=="备泵合同"){
		var projectCode = jQuery('#projectCode').val();
		var contract = "saveContractPump";
		// 判断是编辑还是新增
        if(isId == ""){// 新增合同
        	var d = {
        			"user":{"id":$("#userId").val()},
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }else{
        	var d = {
        			"id": isId,
	        		"createTime": createTime,
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }
	}else if (contractType=="维修合同"){
		var repairDate = jQuery('#repairDate').val();
		if (repairDate==""){
			$.springLemonDialog({
		         content: '请将返厂日期信息完善，保存后还可进行编辑。',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
     		}); 
	 		return ;
 		}
			var projectCode = jQuery('#projectCode').val();
			var repairCode = jQuery('#repairCode').val();
			var repairNumber = jQuery('#repairNumber').val();
			var repairKind = jQuery('#repairKind').val();
			var contract = "saveContractRepair";
			// 判断是编辑还是新增
        if(isId == ""){// 新增合同
        	var d = {
        			"user":{"id":$("#userId").val()},
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "repairCode": repairCode,
                    "repairNumber": repairNumber,
                    "repairKind": repairKind,
                    "repairDate": repairDate,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }else{
        	var d = {
        			"id": isId,
	        		"createTime": createTime,
                	"supplierCode": supplierCode,
                	"demanderCode": demanderCode,
                	"contractType": contractType,
                    "totalSum": totalSum,
                    "signedDate": signedDate,
                    "scheduleDate": scheduleDate,
                    "giveOutDate": giveOutDate,
                    "projectCode": projectCode,
                    "repairCode": repairCode,
                    "repairNumber": repairNumber,
                    "repairKind": repairKind,
                    "repairDate": repairDate,
                    "stop": stop,
                    "freightCharge": freightCharge,
                    "projectName": projectName,
                    "payType": payType,
                    "guaranteePeriod": guaranteePeriod,
                    "breakDuty": breakDuty,
                    "customerId": customerId,
                    "orderUnit": orderUnit,
                    "orderDelegate": orderDelegate,
                    "orderPhone": orderPhone,
                    "industryInfo": industryInfo,
                    "orderAddress": orderAddress,
                    "orderZip": orderZip,
                    "orderArea": orderArea,
                    "invoiceBank": invoiceBank,
  	                "bankAccount": bankAccount,
  	                "taxNumber": taxNumber,
  	                "invoiceAddress": invoiceAddress,
                    "postUnit": postUnit,
                    "postDelegate": postDelegate,
                    "postPhone": postPhone,
                    "postAddress": postAddress,
                    "postZip": postZip,
                    "dutyArea": dutyArea,
                    "dutyOfficer": dutyOfficer,
                    "southNorthArea": southNorthArea,
                    "belongArea": belongArea,
                    "checkKind": checkKind,
                    "productKind": productKind,
                    "signOfficer": signOfficer,
                    "repairOfficer": repairOfficer,
                    "quotationOfficer": quotationOfficer,
                    "operatDepartment": operatDepartment,
                    "operator": operator,
                    "operatDate": operatDate,
                    "memo": memo,
                    "state": state,
                    "cstate": cstate,
            };
        }
	}
	jQuery(".app-btn-submit").attr("disabled", true);
	var id=isId;
	if (id==null||id==''){
		id = 'null';
	}
	if (supplierCode!=''){// 验证供方合同号是否已存在
		var urlValid=window.rootPath+"/ui/contract/validSupplierCode/"+id+"/"+supplierCode;
    	jQuery.ajax({
            url:encodeURI(urlValid),
            cache:false,
            type :"get",
            dataType:"json",
            contentType:"application/json;charset=utf-8",// 解决返回数据中的中文乱码问题
            success:function(data){
            	if(data.codeState==1){
            		$.springLemonDialog({
       		         content: '供方合同号已存在，请修改！',// 确认文本
       		         clickButton: function (sender, modal, index) {// 按钮事件处理
       		             $(this).closeDialog(modal);
       		         }
             		}); 
            		jQuery(".app-btn-submit").attr("disabled", false);
            		return;
            	}
            	if(contractType=="项目合同"){
	            		if (totalSum==""){
  	            		totalSum = 0;
  	            	}
  	            	var url0=window.rootPath+"/ui/contract/validTotalSum/"+id+"/"+totalSum+"/0";
  	         		jQuery.ajax({
  	      	            url:encodeURI(url0),
  	      	            cache:false,
  	      	            type :"get",
  	      	            dataType:"json",
  	      	            contentType:"application/json;charset=utf-8",// 解决返回数据中的中文乱码问题
  	      	            success:function(data){
  	      	            	if(data.sumState==1){
	  	      	            	$.springLemonDialog({
	  	            		         content: '合同总价小于占比划分总金额，请修改！',// 确认文本
	  	            		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  	            		             $(this).closeDialog(modal);
	  	            		         }
	  	                  		}); 
  	      	            		jQuery(".app-btn-submit").attr("disabled", false);
  	      	            		return;
  	      	            	}
	  	      	            var url = window.rootPath + "/ui/contract/" + contract;
	  	                    jQuery.ajax({
	  	                        type: 'post',
	  	                        url: url,
	  	                        data: JSON.stringify(d),
	  	                        dataType: 'json',
	  	                        contentType: 'application/json;charset=utf-8',
	  	                        success: function (data) {
	  	                    		auditContract(data.contractId, data.contractType);
	  	                        }
	  	                    });
  	      	            }
  	         		})
            	}else{
            		var url = window.rootPath + "/ui/contract/" + contract;
                    jQuery.ajax({
                        type: 'post',
                        url: url,
                        data: JSON.stringify(d),
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        success: function (data) {
                    		auditContract(data.contractId, data.contractType);
                        }
                    });
            	}
            }
    	});
	}else{
		if(contractType=="项目合同"){
    		if (totalSum==""){
      		totalSum = 0;
	      	}
	      	var url0=window.rootPath+"/ui/contract/validTotalSum/"+id+"/"+totalSum+"/0";
	   		jQuery.ajax({
		            url:encodeURI(url0),
		            cache:false,
		            type :"get",
		            dataType:"json",
		            contentType:"application/json;charset=utf-8",// 解决返回数据中的中文乱码问题
		            success:function(data){
		            	if(data.sumState==1){
		            		$.springLemonDialog({
 	            		         content: '合同总价小于占比划分总金额，请修改！',// 确认文本
 	            		         clickButton: function (sender, modal, index) {// 按钮事件处理
 	            		             $(this).closeDialog(modal);
 	            		         }
 	                  		});
		            		jQuery(".app-btn-submit").attr("disabled", false);
		            		return;
		            	}
	    	            var url = window.rootPath + "/ui/contract/" + contract;
	                  jQuery.ajax({
	                      type: 'post',
	                      url: url,
	                      data: JSON.stringify(d),
	                      dataType: 'json',
	                      contentType: 'application/json;charset=utf-8',
	                      success: function (data) {
	                  		auditContract(data.contractId, data.contractType);
	                      }
	                  });
		            }
		   		})
		}else{
				var url = window.rootPath + "/ui/contract/" + contract;
		        jQuery.ajax({
		            type: 'post',
		            url: url,
		            data: JSON.stringify(d),
		            dataType: 'json',
		            contentType: 'application/json;charset=utf-8',
		            success: function (data) {
		        		auditContract(data.contractId, data.contractType);
		            }
		        });
		}
	}
};

/**
 * 提交合同审批
 */
var auditContract = function(contractId, contractType) {
	var url = window.rootPath + "/ui/ contract/audit/"
				+ contractId + "/" + contractType;

	jQuery.post(encodeURI(url),
	// jQuery('#form-audit-submit').serialize(),
	{
	"auditorNumber" : jQuery("#auditorNumber").val(),
	"auditor1" : jQuery("#auditor1").val(),
	"auditor2" : jQuery("#auditor2").val(),
	"auditor3" : jQuery("#auditor3").val(),
	"auditor4" : jQuery("#auditor4").val(),
	"auditor5" : jQuery("#auditor5").val(),
	"auditor6" : jQuery("#auditor6").val(),
	"auditor7" : jQuery("#auditor7").val(),
	"auditor8" : jQuery("#auditor8").val()
	
	}, function(data) {
	
	if (data.code == 0) {
		jQuery(".app-btn-submit").attr("disabled", false);
		window.location = window.rootPath + "/ui/contract/";
	}
	});
}
